package des.wangku.operate.task.news.bookssearch;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import des.wangku.operate.standard.swt.AutoSettingCombo;
import des.wangku.operate.standard.swt.freectabfolder.ExcelCTabFolder;
import des.wangku.operate.standard.swt.table.ResultTable;
import des.wangku.operate.standard.task.AbstractTask;
import des.wangku.operate.standard.task.AnnoProjectTask;
import des.wangku.operate.standard.utls.UtilsFile;
import des.wangku.operate.standard.utls.UtilsJsoup;
import des.wangku.operate.standard.utls.UtilsJsoupLink;
import des.wangku.operate.standard.utls.UtilsString;
import des.wangku.operate.standard.utls.UtilsStringFilter;
import des.wangku.operate.task.news.bookssearch.Utils.UnitFirstClass;

import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Spinner;




@AnnoProjectTask(autoLoad = true, group = "书籍检索", name = "从网址中提取书籍", identifier = "P201", expire = false)
public class WKOPETaskbookssearch extends AbstractTask {
	/** 日志 */
	static final Logger logger = LoggerFactory.getLogger(WKOPETaskbookssearch.class);
	Display display = Display.getDefault();
	Group group=null;
	/** 名称 */
	private Text text_project;
	/** 地址 */
	private Text text_url;
	/** 区块标签 */
	private Text text_area;
	/** 书目标签 */
	private Text text_name;
	/**  */
	private Text text_namefilter;
	/**  */
	private Text text_itemurl;
	
	private Text text_last;
	
	private Spinner spinner;
	
	private Button button_autotitle = null;
	
	ExcelCTabFolder excelCTabFolder = null;
	
	/** 下载文件的地址，所在的id或标签 */
	private Text text_file;
	private Text text_targettag;
	private Text text_itemtag;
	private Text text_contenttag;
	private Text text_urlbad;
	private Text pnum_text;
	
	public WKOPETaskbookssearch(Composite parent) {
		super(parent);
	}
	
	
	public WKOPETaskbookssearch(Composite parent, int style) {
		super(parent, style, WKOPETaskbookssearch.class);
		setSize(ACC_CpsWidth, ACC_CpsHeight);
		group = new Group(this, SWT.NONE);
		group.setBounds(5, 5, 845, 523);
		group.setText("项目");
		excelCTabFolder = new ExcelCTabFolder(group, SWT.NONE,"group1");
		excelCTabFolder.initialization();
		excelCTabFolder.setBounds(10, 283, 832, 237);
		
		Group grpSs = new Group(group, SWT.NONE);
		grpSs.setText("参数");
		grpSs.setBounds(10, 21, 832, 214);
		

		Button button = new Button(grpSs, SWT.NONE);
		button.setBounds(620, 60, 68, 23);
		button.setText("检索");
		
		
		
		
		AutoSettingCombo combo_1 = new AutoSettingCombo(grpSs, 0,"图书检索系统");
		combo_1.setBounds(737, 23, 85, 21);
		combo_1.initialization();

		Label label = new Label(grpSs, SWT.NONE);
		label.setAlignment(SWT.RIGHT);
		label.setBounds(10, 26, 37, 13);
		label.setText("来源");
		text_project = new Text(grpSs, SWT.BORDER);
		text_project.setBounds(55, 23, 99, 19);
		

		Label label_1 = new Label(grpSs, SWT.NONE);
		label_1.setAlignment(SWT.RIGHT);
		label_1.setBounds(168, 26, 29, 13);
		label_1.setText("地址");
		text_url = new Text(grpSs, SWT.BORDER);
		text_url.setBounds(203, 26, 485, 19);
		

		Label label_4 = new Label(grpSs, SWT.NONE);
		label_4.setAlignment(SWT.RIGHT);
		label_4.setBounds(10, 65, 37, 13);
		label_4.setText("列表");
		text_area = new Text(grpSs, SWT.BORDER);
		text_area.setBounds(55, 62, 68, 21);

		Label label_2 = new Label(grpSs, SWT.NONE);
		label_2.setAlignment(SWT.RIGHT);
		label_2.setBounds(147, 68, 49, 13);
		label_2.setText("名称标签");
		text_name = new Text(grpSs, SWT.BORDER);
		text_name.setBounds(202, 61, 99, 19);
		

		Label label_6 = new Label(grpSs, SWT.NONE);
		label_6.setAlignment(SWT.RIGHT);
		label_6.setBounds(307, 65, 57, 13);
		label_6.setText("名称过滤");
		text_namefilter = new Text(grpSs, SWT.BORDER);
		text_namefilter.setBounds(370, 62, 101, 19);


		Label label_3 = new Label(grpSs, SWT.NONE);
		label_3.setAlignment(SWT.RIGHT);
		label_3.setBounds(145, 99, 49, 13);
		label_3.setText("链接标签");
		text_itemurl = new Text(grpSs, SWT.BORDER);
		text_itemurl.setBounds(197, 96, 104, 19);

		
		
		
		Label label_5 = new Label(grpSs, SWT.NONE);
		label_5.setAlignment(SWT.RIGHT);
		label_5.setBounds(315, 99, 49, 13);
		label_5.setText("最后章");
		text_last = new Text(grpSs, SWT.BORDER);
		text_last.setBounds(372, 96, 99, 19);

		
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				testing();
			}
		});

		Label label_7 = new Label(grpSs, SWT.NONE);
		label_7.setAlignment(SWT.RIGHT);
		label_7.setBounds(633, 104, 57, 13);
		label_7.setText("线程数量");
		
		spinner = new Spinner(grpSs, SWT.BORDER | SWT.READ_ONLY);
		spinner.setMaximum(20);
		spinner.setMinimum(1);
		spinner.setSelection(1);
		spinner.setBounds(696, 101, 41, 21);
		
		

		Button Button_run = new Button(grpSs, SWT.NONE);
		Button_run.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				runing();
				
			}
		});
		Button_run.setBounds(754, 99, 68, 23);
		Button_run.setText("线程运行");
		
		Button button_1 = new Button(grpSs, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				updatefilestate();
				
			}
		});
		button_1.setBounds(754, 60, 68, 23);
		button_1.setText("更新文件");
		
		Label label_8 = new Label(grpSs, SWT.NONE);
		label_8.setAlignment(SWT.RIGHT);
		label_8.setBounds(38, 129, 85, 13);
		label_8.setText("下载文件标签");
		
		text_file = new Text(grpSs, SWT.BORDER);
		text_file.setBounds(129, 126, 71, 19);
		
		Label label_9 = new Label(grpSs, SWT.NONE);
		label_9.setAlignment(SWT.RIGHT);
		label_9.setBounds(38, 158, 85, 13);
		label_9.setText("列表目标标签");
		label_9.setToolTipText("为空，则为直接链接到列表而，如链接到主页，则用此标签导到列表页");
		
		text_targettag = new Text(grpSs, SWT.BORDER);
		text_targettag.setBounds(129, 155, 71, 19);
		Label label_10 = new Label(grpSs, SWT.NONE);
		label_10.setAlignment(SWT.RIGHT);
		label_10.setBounds(203, 158, 57, 13);
		label_10.setText("章节标签");
		
		text_itemtag = new Text(grpSs, SWT.BORDER);
		text_itemtag.setBounds(266, 155, 71, 19);
		
		Label label_11 = new Label(grpSs, SWT.NONE);
		label_11.setAlignment(SWT.RIGHT);
		label_11.setBounds(343, 158, 57, 13);
		label_11.setText("内容标签");
		
		text_contenttag = new Text(grpSs, SWT.BORDER);
		text_contenttag.setBounds(406, 155, 71, 19);
		button_autotitle = new Button(grpSs, SWT.CHECK);
		button_autotitle.setBounds(499, 158, 99, 16);
		button_autotitle.setText("自动添加篇名");
		
		
		Label label_12 = new Label(grpSs, SWT.NONE);
		label_12.setToolTipText("所有章节链接过滤域名中的关键字，以,分开");
		label_12.setAlignment(SWT.RIGHT);
		label_12.setBounds(203, 191, 62, 13);
		label_12.setText("链接过滤");
		
		text_urlbad = new Text(grpSs, SWT.BORDER);
		text_urlbad.setBounds(266, 185, 98, 19);
		
		Label label_13 = new Label(grpSs, SWT.NONE);
		label_13.setAlignment(SWT.RIGHT);
		label_13.setBounds(477, 99, 49, 13);
		label_13.setText("章数量");
		
		pnum_text = new Text(grpSs, SWT.BORDER);
		pnum_text.setBounds(532, 96, 71, 19);
		
		
		
	}
	/**
	 * 线程运行程序
	 */
	public void runing() {
		ResultTable rt=excelCTabFolder.getSelectResultTable();
		int maxThread=spinner.getSelection();
		int[] all=rt.getChk();
		//List<String> list=rt.getColumnList(true,2);
		List<UnitFirstClass> ufclist=new ArrayList<>();
		for(int f:all) {
			String filename=rt.get(f, 1);
			String url=rt.get(f, 2);
			UnitFirstClass ufc=new UnitFirstClass(filename,url);
			
			ufc.contentkey=text_contenttag.getText().trim();
			
			ufc.downfiletag=text_file.getText().trim();
			ufc.targettag =text_targettag.getText().trim();
			ufc.itemtag=text_itemtag.getText().trim();
			ufc.autotitle=button_autotitle.getSelection();
			ufc.urlbad=text_urlbad.getText();
			ufclist.add(ufc);
		}
		Utils.makelist(maxThread, ufclist);
		
	}
	/**
	 * 提出列表
	 */
	public void testing() {
		ResultTable rt=excelCTabFolder.getSelectResultTable();
		String urlall=text_url.getText();
		
		
		
		
		
		
		//"G:/Download/";
		String webname=text_project.getText();
		String tagArea=text_area.getText();
		String tagname=text_name.getText();
		String tagnamefilter=text_namefilter.getText().trim();
		String tagitemurl=text_itemurl.getText();
		String taglast=text_last.getText();
		String tagpnum=pnum_text.getText();
		
		String[] urlarray=UtilsString.splitString(urlall);
		for(String url:urlarray) {
			Elements es=UtilsJsoup.getElementAll(url, tagArea);
			System.out.println("es:"+es.size());
			if(es.size()==0)return;
			for(Element e:es) {
				List<String> list=new ArrayList<>();
				list.add(webname);//来源
				String name="";
				if(tagname.length()>0) {
					Element f=UtilsJsoup.getElementFirst(e, tagname);
					if(f!=null) {
						name=f.text();
						if(tagnamefilter.length()>0) {
							for(String g:tagnamefilter.split(",")) {
								name=name.replaceAll(g,"");
							}
						}
						name = UtilsStringFilter.filterName(name);
					}
				}
				if(name.length()>0) {
					if(isExistFile(name)) {
						System.out.println("存在文件:"+name);
						continue;
					}
				}
				Element g=UtilsJsoup.getElementFirst(e, tagitemurl);
				String href="";
				if(g!=null) {
					href=UtilsJsoupLink.getHrefFirst(g, tagitemurl);
				}
				String lasttext="";
				if(taglast.length()>0) {
					Element f=UtilsJsoup.getElementFirst(e, taglast);
					if(f!=null)	lasttext=f.text();
				}
				String pnum="";
				if(tagpnum.length()>0) {
					Element f=UtilsJsoup.getElementFirst(e, tagpnum);
					if(f!=null)	pnum=f.text();
					
				}
				if("连载中".equals(lasttext))continue;
				list.add(name);
				list.add(href);
				list.add(lasttext);
				list.add(pnum);
				rt.add(list);
			}
			
			
		}
		
		
		updatefilestate();
	}
	/**
	 * 更新文件状态，是否存在文件
	 */
	void updatefilestate() {
		ResultTable rt=excelCTabFolder.getSelectResultTable();
		int len=rt.getItemCount();
		for(int i=0;i<len;i++) {
			String name=rt.get(i, 1);
			File file = UtilsFile.getFilePath(Consts.ACC_SavePath, name + ".txt");
			if(file==null)continue;
			String val=file.getAbsolutePath();
			rt.set(i, 5, val);
		}
	}
	static final boolean isExistFile(String name) {
		File file =existFile(name);
		return file!=null;
	}
	static final File existFile(String name) {
		return UtilsFile.getFilePath(Consts.ACC_Path, name + ".txt");
	}
	@Override
	public String getTask_Name() {
		return null;
	}

	@Override
	public String getTask_Identifier() {
		return null;
	}
	public void disposeOriginalProject() {

	}
	public void disposeResources() {

	}
}
